﻿Imports System.Configuration.ConfigurationManager
Imports System.Data
Imports System.Data.SqlClient

Partial Class MantemientosMenu
    Inherits System.Web.UI.Page
    Dim mantenimiento As New MantenimientoEquipo

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            cargarMarca()
            cargarCodigoCorrelativo()
            cargarGridview()
            cargarGridviewMarca()
            cargarCodigoCorrelativoMarca()
        End If
    End Sub

    Sub cargarMarca()

        Dim StrQuery As String = "select cod_marca cod, Name_marca 'desc'  from dbo.tb_marca where estado='A' order by Name_marca "

        Dim dt As DataTable = mantenimiento.ListadoParametro(StrQuery)
        CboMarca.DataSource = dt
        CboMarca.DataTextField = "desc"
        CboMarca.DataValueField = "cod"
        CboMarca.DataBind()

    End Sub

    Sub cargarCodigoCorrelativo()

        Dim cod As String = mantenimiento.generaCodigo("select  MAX(cod_equipo) + 1 corr  from dbo.tb_equipo")
        lblCodigo.Text = cod.ToString()

    End Sub

    Sub cargarGridview()
        Dim StrQuery As String = " select  cod_equipo Cod ,ma.cod_marca cod_marca ,ma.Name_marca Marca " & _
                                " ,Equip_desc 'desc' ,precio  'precio' " & _
                                " from dbo.tb_equipo eq, dbo.tb_marca ma " & _
                                " where eq.cod_marca = ma.cod_marca " & _
                                " AND eq.estado = 'A' "

        Dim dt As DataTable = mantenimiento.ListadoParametro(StrQuery)
        Session.Add("dt", dt)
        GridView1.DataSource = dt
        GridView1.DataBind()


    End Sub

    Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        LLenarDatosSession()
        upTab1.Update()
    End Sub

    Sub LLenarDatosSession()
        Dim dt As DataTable = CType(Session.Item("dt"), DataTable)
        GridView1.DataSource = dt
        GridView1.DataBind()


    End Sub
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
        Dim i As Integer = GridView1.SelectedIndex
        Dim dt As DataTable = CType(Session.Item("dt"), DataTable)

        Dim pagina As Integer = GridView1.PageIndex
        i = i + (pagina * 10)

        Dim cod As String = dt.Rows(i).Item("Cod").ToString()
        Dim cod_marca As String = dt.Rows(i).Item("Cod_marca").ToString()
        Dim desc As String = dt.Rows(i).Item("desc").ToString()
        Dim Precio As String = Format(CDbl(dt.Rows(i).Item("precio").ToString()), "##,##0.00")

        lblCodigo.Text = cod.ToString
        CboMarca.SelectedValue = cod_marca
        txtDescripcion.Text = desc
        txtPrecio.Text = Precio

        btnNuevo.Text = "Actualizar"
        fileImagen.Visible = False
        Label5.Visible = False

        btnActualizar.Visible = True

        upTab1.Update()

    End Sub
    Protected Sub btnActualizar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnActualizar.Click

        Limpiar()
        upTab1.Update()
    End Sub

    Sub Limpiar()
        cargarMarca()
        cargarCodigoCorrelativo()
        cargarGridview()
        btnActualizar.Visible = False
        btnNuevo.Text = "Nuevo"
        txtDescripcion.Text = ""
        txtPrecio.Text = ""
        fileImagen.Visible = True
        Label5.Visible = True
    End Sub

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        Dim i As Integer = e.RowIndex
        Dim dt As DataTable = CType(Session.Item("dt"), DataTable)

        Dim pagina As Integer = GridView1.PageIndex
        i = i + (pagina * 10)

        Dim cod As String = dt.Rows(i).Item("Cod").ToString()

        mantenimiento.EliminarEquipo(cod)

        Limpiar()
        upTab1.Update()
    End Sub


    Protected Sub btnNuevo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNuevo.Click

        If btnNuevo.Text = "Actualizar" Then

         
            Try
                Dim cod As String = lblCodigo.Text.Trim
                Dim marca As String = CboMarca.SelectedValue.ToString().Trim
                Dim desc As String = txtDescripcion.Text
                Dim precio As Double = CDbl(txtPrecio.Text.Trim)
                mantenimiento.ActualizarEquipo(cod, marca, desc, precio)
                Limpiar()

            Catch ex As Exception

                MsgBox("Formato de precio incorrecto")

            End Try


        Else

            Try
                Dim cod As String = lblCodigo.Text.Trim
                Dim marca As String = CboMarca.SelectedValue.ToString().Trim
                Dim desc As String = txtDescripcion.Text
                Dim precio As Double = CDbl(txtPrecio.Text.Trim)
                mantenimiento.InsertarEquipo(marca, desc, precio, "A")
                Limpiar()

            Catch ex As Exception

                MsgBox("Formato de precio incorrecto")

            End Try

        End If

        upTab1.Update()
    End Sub



    '----------------------------COMIENZA TAB 2 MARCA


    Sub cargarGridviewMarca()

        Dim dt As DataTable = mantenimiento.ListadoParametro("   select   cod_marca cod_marca ,Name_marca 'desc'  " & _
                                                             " from dbo.tb_marca " & _
                                                     "where estado='A'")

        Session.Add("dtMarca", dt)
        GrvModelo.DataSource = dt
        GrvModelo.DataBind()
        upTab2.Update()

    End Sub
    Sub cargarCodigoCorrelativoMarca()

        Dim cod As String = mantenimiento.generaCodigo("select COUNT(*) + 1 corr from dbo.tb_marca ")

        If CType(cod, Integer) <= 9 Then

            cod = "M00" & cod

        Else

            cod = "M0" & cod

        End If

        lblCodModelo.Text = cod.ToString()
        upTab2.Update()
    End Sub
    Protected Sub GrvModelo_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GrvModelo.PageIndexChanging
        GrvModelo.PageIndex = e.NewPageIndex
        LLenarDatosSessionMarca()
        upTab2.Update()
    End Sub
    Sub LLenarDatosSessionMarca()
        Dim dt As DataTable = CType(Session.Item("dtMarca"), DataTable)
        GrvModelo.DataSource = dt
        GrvModelo.DataBind()
    End Sub

    

    Protected Sub imgActualizarModelo_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles imgActualizarModelo.Click
        limpiarMarca()
    End Sub

    Sub limpiarMarca()
        btnNuevoModelo.Text = "Nuevo"
        cargarCodigoCorrelativoMarca()
        cargarGridviewMarca()
        imgActualizarModelo.Visible = False
        txtModelo.Text = ""
        upTab2.Update()
    End Sub

    Protected Sub GrvModelo_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GrvModelo.RowDeleting
        Dim i As Integer = e.RowIndex
        Dim cod As String = GrvModelo.Rows(i).Cells(2).Text.ToString()

        mantenimiento.EliminarModelo(cod)
        limpiarMarca()

    End Sub


    Protected Sub GrvModelo_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles GrvModelo.SelectedIndexChanged
        Dim i As Integer = GrvModelo.SelectedIndex

        Dim cod As String = GrvModelo.Rows(i).Cells(2).Text.ToString()
        Dim des As String = GrvModelo.Rows(i).Cells(3).Text.ToString()

        lblCodModelo.Text = cod
        txtModelo.Text = des
        btnNuevoModelo.Text = "Actualizar"
        imgActualizarModelo.Visible = True
        upTab2.Update()
    End Sub

    Protected Sub btnNuevoModelo_Click(sender As Object, e As System.EventArgs) Handles btnNuevoModelo.Click

        If btnNuevoModelo.Text = "Nuevo" Then

            Dim cod As String = lblCodModelo.Text
            Dim desc As String = txtModelo.Text
            mantenimiento.insertarModelo(cod, desc, "A")
            limpiarMarca()

        Else

            Dim cod As String = lblCodModelo.Text
            Dim desc As String = txtModelo.Text
            mantenimiento.ActualizarModelo(cod, desc)
            limpiarMarca()

        End If

    End Sub



End Class
